home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-16 | 11.7 KB | 339 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: GroupCmd.cpp
- // Release Version: $ ODF 1 $
- //
- // Author: Mary Boetcher
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "ODFDraw.hpp"
-
- #ifndef GROUPCMD_H
- #include "GroupCmd.h"
- #endif
-
- #ifndef DRAWCONT_H
- #include "DrawCont.h"
- #endif
-
- #ifndef DRAWPART_H
- #include "DrawPart.h"
- #endif
-
- #ifndef DRAWSEL_H
- #include "DrawSel.h"
- #endif
-
- #ifndef BASESHP_H
- #include "BaseShp.h"
- #endif
-
- #ifndef GROUPSHP_H
- #include "GroupShp.h"
- #endif
-
- // for cGroup
- #ifndef DEFINES_K
- #include "Defines.k"
- #endif
-
- // ----- FrameWork Includes -----
-
- #ifndef FWPRESEN_H
- #include "FWPresen.h"
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfdrawcommand
- #endif
-
- FW_DEFINE_AUTO(CGroupContent)
- FW_DEFINE_AUTO(CGroupShapesCommand)
- FW_DEFINE_AUTO(CUngroupShapesCommand)
-
- //========================================================================================
- // class CGroupContent
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CGroupContent constructor
- //----------------------------------------------------------------------------------------
- CGroupContent::CGroupContent(Environment* ev, CDrawContent* other)
- : CDrawContent(ev, other)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupContent destructor
- //----------------------------------------------------------------------------------------
- CGroupContent::~CGroupContent()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupContent::GetShapeList
- //----------------------------------------------------------------------------------------
- CShapeCollection* CGroupContent::GetShapeList()
- {
- return fShapeList;
- }
-
- //========================================================================================
- // CGroupShapesCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CGroupShapesCommand constructor
- //----------------------------------------------------------------------------------------
- CGroupShapesCommand::CGroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
- : FW_CCommand(ev, cGroup, frame, FW_kCanUndo),
- fDrawSelection(selection),
- fGroupShape(NULL),
- fGroupedContent(NULL)
- {
- SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoGroupMsg, kRedoGroupMsg);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupShapesCommand destructor
- //----------------------------------------------------------------------------------------
- CGroupShapesCommand::~CGroupShapesCommand()
- {
- FW_START_DESTRUCTOR
- delete fGroupedContent;
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupShapesCommand::CommitUndone
- //----------------------------------------------------------------------------------------
- void CGroupShapesCommand::CommitUndone(Environment* ev)
- {
- // Delete the group shape, but not the shapes it contains
- fGroupShape->EmptyShapes(false); // remove shapes, but don't delete them
- delete fGroupShape; // deletes the shapeList
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupShapesCommand::DoIt
- //----------------------------------------------------------------------------------------
- void CGroupShapesCommand::DoIt(Environment* ev)
- {
- // Save Undo state - copy selected shapes
- fGroupedContent = FW_NEW(CGroupContent, (ev, fDrawSelection->GetDrawContent(ev)));
-
- // Combine selected shapes into a new group shape
- CShapeCollection* shapeList = fGroupedContent->GetShapeList();
- fGroupShape = FW_NEW(CGroupShape, (shapeList));
-
- this->GroupShapes(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupShapesCommand::UndoIt
- //----------------------------------------------------------------------------------------
- void CGroupShapesCommand::UndoIt(Environment* ev)
- {
- // Ungroup the shapes
- CShapeCollection* shapeList = fGroupedContent->GetShapeList();
- CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
- CDrawPartContent* partContent = drawPart->GetDrawContent();
-
- // Add each shape back into the part's list
- CShapeCollectionIterator it(shapeList);
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- partContent->AddShape(ev, shape, fGroupShape); // insert before the group shape
- }
-
- // Remove group shape from the part
- partContent->RemoveShape(ev, fGroupShape);
-
- // Select the shapes
- fDrawSelection->SelectContent(ev, fGroupedContent);
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupShapesCommand::RedoIt
- //----------------------------------------------------------------------------------------
- void CGroupShapesCommand::RedoIt(Environment* ev)
- {
- this->GroupShapes(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CGroupShapesCommand::GroupShapes
- //----------------------------------------------------------------------------------------
- void CGroupShapesCommand::GroupShapes(Environment* ev)
- {
- // Remove shapes from the selection
- fDrawSelection->CloseSelection(ev);
-
- // Remove each shape from the part's list (but don't detach it)
- CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
- CDrawPartContent* partContent = drawPart->GetDrawContent();
- CShapeCollection* shapeList = fGroupedContent->GetShapeList();
-
- CShapeCollectionIterator it(shapeList);
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- partContent->RemoveShape(ev, shape);
- }
-
- // Add the new group shape to the part and select it
- drawPart->AddShapeToPart(ev, fGroupShape);
- fDrawSelection->AddToSelection(ev, fGroupShape, true);
- }
-
- //========================================================================================
- // CUngroupShapesCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand constructor
- //----------------------------------------------------------------------------------------
- CUngroupShapesCommand::CUngroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
- : FW_CCommand(ev, cUngroup, frame, FW_kCanUndo),
- fDrawSelection(selection),
- fGroupList(NULL)
- {
- // Copy selected shapes that are group shapes
- fGroupList = FW_NEW(CShapeCollection, ());
- CDrawContentShapeIterator it(fDrawSelection->GetDrawContent(ev));
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- if (shape->GetShapeType() == kGroupShape)
- fGroupList->AddLast(shape);
- }
-
- SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoUngroupMsg, kRedoUngroupMsg);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand destructor
- //----------------------------------------------------------------------------------------
- CUngroupShapesCommand::~CUngroupShapesCommand()
- {
- FW_START_DESTRUCTOR
- delete fGroupList;
- }
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand::CommitDone
- //----------------------------------------------------------------------------------------
- void CUngroupShapesCommand::CommitDone(Environment* ev)
- {
- // Delete the saved group shapes, since they're obsolete
- CBaseShape* shape;
- while ((shape = fGroupList->First()) != NULL)
- {
- fGroupList->Remove(shape);
- ((CGroupShape*)shape)->EmptyShapes(false); // don't delete shapes
- delete shape;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand::DoIt
- //----------------------------------------------------------------------------------------
- void CUngroupShapesCommand::DoIt(Environment* ev) // Override
- {
- if (fGroupList->Count() == 0) return; // nothing to do
-
- // Remove shapes from the selection
- fDrawSelection->CloseSelection(ev);
-
- // Iterate saved shapes and ungroup each one
- CShapeCollectionIterator it(fGroupList);
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- this->UngroupShape(ev, (CGroupShape*)shape);
- }
-
- fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
- }
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand::UndoIt
- //----------------------------------------------------------------------------------------
- void CUngroupShapesCommand::UndoIt(Environment* ev) // Override
- {
- if (fGroupList->Count() == 0) return; // nothing to do
-
- // Remove shapes from the selection
- fDrawSelection->CloseSelection(ev);
-
- // Iterate saved shapes and regroup each one
- CShapeCollectionIterator it(fGroupList);
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- this->RegroupShape(ev, (CGroupShape*)shape);
- }
-
- fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
- }
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand::RedoIt
- //----------------------------------------------------------------------------------------
- void CUngroupShapesCommand::RedoIt(Environment* ev) // Override
- {
- this->DoIt(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand::UngroupShape
- //----------------------------------------------------------------------------------------
- void CUngroupShapesCommand::UngroupShape(Environment* ev, CGroupShape* groupShape)
- {
- CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
- CDrawPartContent* partContent = drawPart->GetDrawContent();
-
- // Remove the group shape itself from the part
- CBaseShape* followingShape = partContent->GetShapeAfter(groupShape);
- partContent->RemoveShape(ev, groupShape);
-
- // Iterate the shapes in the group and add each to the part, in the right position
- CShapeCollectionIterator it(groupShape->GetShapeList());
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- partContent->AddShape(ev, shape, followingShape);
- fDrawSelection->AddToSelection(ev, shape, false);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CUngroupShapesCommand::RegroupShape
- //----------------------------------------------------------------------------------------
- void CUngroupShapesCommand::RegroupShape(Environment* ev, CGroupShape* groupShape)
- {
- CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
- CDrawPartContent* partContent = drawPart->GetDrawContent();
-
- // Remove the shapes from the part
- CBaseShape* followingShape = NULL;
- CShapeCollectionIterator it(groupShape->GetShapeList());
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- followingShape = partContent->GetShapeAfter(shape);
- partContent->RemoveShape(ev, shape);
- }
-
- // Add the group shape back into the part, in the right position
- partContent->AddShape(ev, groupShape, followingShape);
- fDrawSelection->AddToSelection(ev, groupShape, false);
- }
-
-